#!/usr/bin/env python
# coding: utf-8

# 将FASTA的结果输出为PAML

import sys
import argparse
from Bio import SeqIO

parser = argparse.ArgumentParser(
    description='''将fasta序列比对结果转化为paml的输入
    用法:
    alifa2nuc.py -i ali.fna -o ali_mRNA.nuc 
    ''')

parser.add_argument('-i',
                help='必须给定，输入比对mRNA的结果')


parser.add_argument('-o',
                help='必须给定，输出文件的路径')

args = parser.parse_args()

if not args.i or not args.o:
    parser.print_help()
    sys.exit()


infile = args.i

outfile  = open(str(args.o),'w')





line_lista  =[]


def inbed(s,n,side = 'r'):
	s = str(s)
	bn = n - len(s)
	if bn < 0:
		raise BaseException('exceed limitation')
	l = ''

	if side =='r':
		for i in range(bn):
			l += ' '
		for i in s:
			l += i
	else:
		for i in s:
			l += i
		for i in range(bn):
			l += ' '

	return l




n = 0
for i in SeqIO.parse(infile,'fasta'):
	n+=1
	line_lista.append('')
	line_lista.append(str(i.name))
	l = len(str(i.seq))
	line = ''
	t = 0
	for j in str(i.seq):


		line = line+j

		t += 1

		if t % 60==0:
			line_lista.append(line+inbed(t,8))

			line = ''
			continue

		if t % 10 ==0:
			line = line + ' '
	if line != '':
		line_lista.append(inbed(line,65,'l')+inbed(t,8))

outfile.write(inbed(n,4)+inbed(l,6)+inbed('GC',4)+'\n')

for i in line_lista:
	outfile.write(i+'\n')
outfile.close()